package it.univpm.diiga.memorysim.views;

import java.util.ArrayList;

import it.univpm.diiga.memorysim.data.DataPlugin;
import it.univpm.diiga.memorysim.kernel.shared.Process;

import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DragSource;
import org.eclipse.swt.dnd.DragSourceAdapter;
import org.eclipse.swt.dnd.DragSourceEvent;
import org.eclipse.swt.dnd.DropTarget;
import org.eclipse.swt.dnd.DropTargetAdapter;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;


import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.swt.graphics.Rectangle;

public class MemoryCentralInitialSituation extends ViewPart {

	
	public static final String ID = "it.univpm.diiga.memorysim.views.MemoryCentralInitialSituation"; 
	private Composite top = null;
	private ScrolledComposite sc1 = null;
	private Composite c1 = null;
	private int NmemC=0;
	private int NmemS=0;
	private Composite sc;
	
	private ArrayList<Integer> CellMem=new ArrayList<Integer>();
	private ArrayList<Integer> CellSwap=new ArrayList<Integer>();
	private String[] DataMem;
	private String[] DataSwap;
	
	public void createPartControl(Composite parent) {

		int lenghtS=0;
		int lenghtC=0;
		if(DataPlugin.blockdata>=1){
			NmemC=(DataPlugin.memorycentraldata*1024)/DataPlugin.blockdata;
			NmemS=(DataPlugin.swapdata*1024)/DataPlugin.blockdata;
		}
		DataSwap= new String[NmemS];
		DataMem= new String[NmemC];
		lenghtS=(NmemS/9)*50;
		lenghtC=(NmemC/9)*50;
		GridLayout gridLayout21 = new GridLayout();
		gridLayout21.numColumns = 10;
		gridLayout21.makeColumnsEqualWidth = false;
		GridData gridData11 = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gridData11.heightHint = -1;
		gridData11.horizontalAlignment = GridData.FILL;
		gridData11.verticalAlignment = GridData.FILL;
		gridData11.grabExcessHorizontalSpace = true;
		gridData11.grabExcessVerticalSpace = true;
		gridData11.horizontalIndent = 0;
		gridData11.widthHint = -1;
		GridData gridData = new GridData();
		gridData.grabExcessVerticalSpace = false;
		
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 3;
		
		top = new Composite(parent, SWT.NONE);
		top.setLayout(null);
		//top.setSize(600, lenght);
		
		Label l01 = new Label(top,SWT.NONE);
		l01.setText("Lista Processi");
		l01.setBounds(new Rectangle(5, 20, 70, 15));
		Label l03 = new Label(top,SWT.NONE);
		l03.setText("Swap");
		l03.setBounds(new Rectangle(127, 19, 40, 15));
		Label l02 = new Label(top,SWT.NONE);
		l02.setText("Memoria centrale");
		l02.setBounds(new Rectangle(474, 21, 100, 15));
		
	//inizio sorgente
		final Tree tree = new Tree(top, SWT.NONE);
		tree.setBounds(new Rectangle(5, 41, 115, 312));
		for(int i=0;i<DataPlugin.getlistProcess().size();i++){
			Process p = DataPlugin.getlistProcess().get(i);
			for(int j=1;j<=p.getTableDim();j++){
				TreeItem proc1 = new TreeItem(tree, SWT.NONE);
				String data="";
				if(DataPlugin.SimSelected=="Paginazione")data="P:"+p.getPID()+" Pag:"+j; 	
				if(DataPlugin.SimSelected=="Segmentazione")data="P:"+p.getPID()+" Seg:"+j;
				proc1.setText(data);
			}	
		}
			
		TreeItem proc6 = new TreeItem(tree, SWT.NONE);
		proc6.setText(" Cancella ");
		DragSource ds = new DragSource(tree, DND.DROP_MOVE);
		ds.setTransfer(new Transfer[] { TextTransfer.getInstance() });
		ds.addDragListener(new DragSourceAdapter() {
			public void dragSetData(DragSourceEvent event) {
		        event.data = tree.getSelection()[0].getText();
		    }
		});
		
    // fine sorgente
		createSc1();
		
		for (int j = 0; j < NmemS; j++) {
			Group group = new Group(c1, SWT.NONE);
		    group.setLayoutData(gridData);
		    group.setLayout(gridLayout);
			final int init=DataPlugin.blockdata*j;
			CellSwap.add(init);
			final int end=init+DataPlugin.blockdata-1;
			group.setText(init+" - "+end);
			final Label l11 = new Label(group, SWT.NONE);
			l11.setText(LoadDataSwap(j));
			l11.addMouseListener(new MouseListener(){
				public void mouseDoubleClick(MouseEvent e) { }
				public void mouseDown(MouseEvent e) {
					if(e.button==3){
						Shell shell=PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
					    Menu menu = new Menu(shell, SWT.POP_UP);
					    MenuItem item = new MenuItem(menu, SWT.PUSH);
					    item.setText("Cancella");
					    item.addSelectionListener(new SelectionListener(){
					    	
							public void widgetDefaultSelected(SelectionEvent e) { }
							public void widgetSelected(SelectionEvent e) {
								  l11.setText("               ");		
								  AddElement1(null,init,end);
							}
					    });
					    l11.setMenu(menu);
					}
					
				}
				
				public void mouseUp(MouseEvent e) {	}
			});
			DropTarget dt1 = new DropTarget(l11, DND.DROP_MOVE);
			dt1.setTransfer(new Transfer[] { TextTransfer.getInstance() });
			dt1.addDropListener(new DropTargetAdapter() {
				public void drop(DropTargetEvent event) {
					String prova=(String) event.data;
				    if(prova.equals(" Cancella ")){
				    		  l11.setText("               ");
				    		  AddElement1(null,init,end);}
				    	else{ 
				    		  l11.setText(prova);
				    		  AddElement1(prova,init,end);
				    	  }				    	  
				  	}
				});
		    }
		sc1.setContent(c1);
		sc1.setExpandHorizontal(true);
		sc1.setExpandVertical(true);
		sc1.setMinSize(950, lenghtS);
		
		
		sc = new ScrolledComposite(top, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
		sc.setLayout(new GridLayout());
		Composite c = new Composite(sc, SWT.NONE);
		c.setLayout(gridLayout21);
		sc.setBounds(new Rectangle(474, 41, 323, 313));
		for (int i = 0; i < NmemC; i++) {
		    Group group = new Group(c, SWT.NONE);
		    group.setLayoutData(gridData);
		    group.setLayout(gridLayout);
			final int init=DataPlugin.blockdata*i;
			CellMem.add(init);
			final int end=init+DataPlugin.blockdata-1;
			group.setText(init+" - "+end);
			final Label l1 = new Label(group, SWT.NONE);
			l1.setText(LoadDataMem(i));
			l1.addMouseListener(new MouseListener(){
				public void mouseDoubleClick(MouseEvent e) { }
				public void mouseDown(MouseEvent e) {
					if(e.button==3){
						Shell shell=PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
					    Menu menu = new Menu(shell, SWT.POP_UP);
					    MenuItem item = new MenuItem(menu, SWT.PUSH);
					    item.setText("Cancella");
					    item.addSelectionListener(new SelectionListener(){
					    	
							public void widgetDefaultSelected(SelectionEvent e) { }
							public void widgetSelected(SelectionEvent e) {
								  l1.setText("               ");		
								  AddElement(null,init,end);
							}
					    });
					    l1.setMenu(menu);
					}
					
				}
				
				public void mouseUp(MouseEvent e) {	}
			});
			DropTarget dt = new DropTarget(l1, DND.DROP_MOVE);
			dt.setTransfer(new Transfer[] { TextTransfer.getInstance() });
			dt.addDropListener(new DropTargetAdapter() {
				public void drop(DropTargetEvent event) {
			    	  String prova=(String) event.data;
			    	  if(prova.equals(" Cancella ")){ 
			    		  l1.setText("               ");
			    		  AddElement(null,init,end);
			    		  }
			    	  else{
			    		  l1.setText(prova);
			    		  AddElement(prova,init,end);
			    		  }
			      	}
			    });
			}
		((ScrolledComposite) sc).setContent(c);
		((ScrolledComposite) sc).setExpandHorizontal(true);
		((ScrolledComposite) sc).setExpandVertical(true);
		((ScrolledComposite) sc).setMinSize(950, lenghtC);
	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}

	/**
	 * This method initializes sc1	
	 *
	 */
	private void createSc1() {
		sc1 = new ScrolledComposite(top, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
		sc1.setLayout(new GridLayout());
		sc1.setBounds(new Rectangle(126, 41, 334, 313));
		createC1();
	}


	/**
	 * This method initializes c1	
	 *
	 */
	private void createC1() {
		GridLayout gridLayout31 = new GridLayout();
		gridLayout31.numColumns = 10;
		gridLayout31.makeColumnsEqualWidth = false;
		c1 = new Composite(sc1, SWT.NONE);
		c1.setLayout(gridLayout31);
	}

	
		
	public void AddElement1(String dati,int in, int end){
		//SWAP
		int index=-1;
		for(int i=0;i<CellSwap.size();i++){
			if(CellSwap.get(i)==in) index=i;
		}
		DataSwap[index]=dati;
		DataPlugin.saveInitialSwap(DataSwap);
	}
	
	public void AddElement(String dati,int in, int end){
		//RAM
		int index=-1;
		for(int i=0;i<CellMem.size();i++){
			if(CellMem.get(i)==in) index=i;
		}
		DataMem[index]=dati;
		DataPlugin.saveInitialMemory(DataMem);
	}
	
	public String LoadDataMem(int i){
		String dato="                    ";
		if(DataPlugin.CentralMemoryP!=null){
			if(DataPlugin.CentralMemoryP.size()>i){
				String nuovo=DataPlugin.CentralMemoryP.get(i);
				if(nuovo!=null && nuovo!="null"){
					dato=nuovo;
				}
			}
		}
		return dato;
	}
	
	public String LoadDataSwap(int i){
		String dato="                    ";
		if(DataPlugin.SwapMemoryP!=null){
			if(DataPlugin.SwapMemoryP.size()>i){
				String nuovo=DataPlugin.SwapMemoryP.get(i);
				if(nuovo!=null && nuovo!="null"){
					dato=nuovo;
				}
			}
		}
		return dato;
	}
}

